package gui;

import dataconnect.Investment;
import dataconnect.SqlConnection;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;

/**
 * description: Message
 * 大部分按钮点击事件处理
 * 
 * @author what-is-me
 * @date 2022/1/3 23:37
 */
public class Message extends Thread {
    public static String msg = new String();
    public static String args[];
    SqlConnection sql;

    public Message(SqlConnection sql) {
        /**
         * Description: Message
         * 
         * @param sql:dataconnect.SqlConnection
         * @return {@link null}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:38
         */
        super();
        this.sql = sql;
    }

    public void run() {
        /**
         * Description: run
         * 
         * @param :
         * @return
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:38
         */
        String tmp;
        while (true) {
            if (!msg.equals("")) {
                tmp = new String(msg);
                msg = new String();
                if (!check(args)) {
                    JOptionPane.showMessageDialog(null, "请不要sql注入", "错误",
                            JOptionPane.ERROR_MESSAGE);
                    continue;
                }
                if (tmp.equals("login")) {
                    if (sql.password.login(args[0], args[1])) {
                        JLabel l = new JLabel("<html><body><p align=\"center\">欢迎<br>进入投资管理系统!</p></body></html>",
                                JLabel.CENTER);
                        l.setFont(new Font("宋体", Font.BOLD, 30));
                        MainWindow.update(l);
                        try {
                            sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MainWindow.update(new MainPage(sql));
                        MainWindow.Larger();
                        // MainWindow.setSizeChangable();
                    } else {
                        JOptionPane.showMessageDialog(null, "登入失败.", "错误",
                                JOptionPane.WARNING_MESSAGE);
                    }
                } else if (tmp.equals("signin")) {
                    if (sql.password.signin(args[0], args[1])) {
                        JOptionPane.showMessageDialog(null, "注册成功.", "提示",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "注册失败.", "错误",
                                JOptionPane.ERROR_MESSAGE);
                    }
                } else if (tmp.equals("byyear")) {
                    Query.tModel.setDataVector(sql.inv.queryYear(args), Investment.tag);
                } else if (tmp.equals("byname")) {
                    Query.tModel.setDataVector(sql.inv.queryName(args), Investment.tag);
                } else if (tmp.equals("submit")) {
                    if (sql.inv.add(args)) {
                        String info = new String();
                        for (int i = 0; i < 6; i++) {
                            info += String.format("<tr><th>%s</th><th>%s</th></tr>", Investment.tag[i], args[i]);
                        }
                        JOptionPane.showMessageDialog(null,
                                "<html><body><p>成功添加条目.<br>详细信息:</p><table>" + info
                                        + "</table></body></html>",
                                "提示",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "信息添加失败.", "错误",
                                JOptionPane.ERROR_MESSAGE);
                    }
                    AllInvestment.update();
                    Earnings.update();
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    private boolean check(String s) {
        /**
         * Description: check
         * 检查输入，防止sql注入
         * 
         * @param s:java.lang.String
         * @return {@link boolean}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:38
         */
        for (char c : s.toCharArray()) {
            if (c == ';' || c == '\'' || c == '"')
                return false;
        }
        return s.indexOf("null", 0) == -1;
    }

    private boolean check(String[] args) {
        /**
         * Description: check
         * 检查输入，防止sql注入
         * 
         * @param args:java.lang.String
         * @return {@link boolean}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:39
         */
        for (String s : args)
            if (!check(s))
                return false;
        return true;
    }
}
